package com.android.clinic.app.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.clinic.app.ClinicApplication;
import com.android.clinic.app.R;
import com.android.clinic.app.ui.widget.dialog.LoadingDialog;
import com.android.clinic.app.utils.AppManager;
import com.android.clinic.app.utils.Toasty;
import com.android.clinic.app.utils.Utils;
import com.gyf.barlibrary.ImmersionBar;
import com.kennyc.view.MultiStateView;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

/**
 * Created by cheng on 2018/1/26.
 */
public abstract class BaseActivity extends RxAppCompatActivity implements View.OnClickListener {

    private MultiStateView mMultiStateView;

    protected LoadingDialog loading;
    protected ImmersionBar mImmersionBar;

    private View searchTitle, textTitle;
    protected ImageView ivBack, ivAll, ivClose;
    protected TextView tvTitle, tvLeft, tvRight, tvAll;
    protected EditText etSearch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_base);
        initTitleBar();
        View bindView = getLayoutInflater().inflate(getLayoutResId(), null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        bindView.setLayoutParams(params);

        mMultiStateView = (MultiStateView) findViewById(R.id.fragment_base_main);
        LinearLayout convertView = (LinearLayout) findViewById(R.id.fragment_base_content);
        convertView.addView(bindView);

        loading = new LoadingDialog(this);

        if (isImmersionBarEnabled())
            initImmersionBar();

        initView(savedInstanceState);
        loadData();
    }

    private void initTitleBar() {
        searchTitle = findViewById(R.id.search_title);
        textTitle = findViewById(R.id.text_title);
        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivAll = (ImageView) findViewById(R.id.iv_all);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvRight = (TextView) findViewById(R.id.tv_right);
        tvAll = (TextView) findViewById(R.id.tv_all);
        etSearch = (EditText) findViewById(R.id.et_search);
        ivClose = (ImageView) findViewById(R.id.iv_close);

        ivBack.setOnClickListener(this::onBackClickListener);
        ivAll.setOnClickListener(this::onRightClickListener);
        tvLeft.setOnClickListener(v -> {
            if ("取消".equals(tvLeft.getText().toString())) {
                onBackClickListener(v);
            }
        });
        tvRight.setOnClickListener(this::onRightClickListener);
        tvAll.setOnClickListener(v -> {
            if ("取消".equals(tvAll.getText().toString())) {
                onBackClickListener(v);
            }
        });
    }

    protected void initImmersionBar() {
        //在BaseActivity里初始化
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
        mImmersionBar.statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在BaseActivity里销毁
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }

    @Override
    public void onClick(View v) {
        if (!Utils.isFastClick()) {
            widgetClick(v);
        }
    }

    /**
     * 控件点击事件
     * @param v
     */
    protected void widgetClick(View v){

    }

    /**
     * 模拟一个账号登录
     * @param account 用户userId
     */
    protected void angoraLogin(String account) {
        ClinicApplication.getInstance().getAngoraAPI().login2(
                ClinicApplication.getInstance().getAppId(), account, "_no_need_token", 0, "" , 5, 1);
    }

    /**
     * 设置标题
     * @param title 标题文字不为空，带有文字的标题栏；否则，显示其他标题栏
     */
    protected void setTitleBarTitle(String title) {
        if (!TextUtils.isEmpty(title)) {
            textTitle.setVisibility(View.VISIBLE);
            ImmersionBar.setTitleBar(this, textTitle);
            tvTitle.setText(title);
        } else {
            searchTitle.setVisibility(View.VISIBLE);
            ImmersionBar.setTitleBar(this, searchTitle);
        }
    }

    /**
     * 设置标题栏左边文字
     * @param text
     */
    protected void setTitleBarLeftText(CharSequence text) {
        if (!TextUtils.isEmpty(text)) {
            tvLeft.setVisibility(View.VISIBLE);
            tvLeft.setText(text);
        }
    }

    /**
     * 设置标题栏右边文字
     * @param text
     */
    protected void setTitleBarRightText(CharSequence text) {
        if (!TextUtils.isEmpty(text)) {
            tvRight.setVisibility(View.VISIBLE);
            tvRight.setText(text);
        }
    }

    /**
     * 设置标题栏左边图片资源
     * @param resId
     */
    protected void setTitleBarLeftResource(int resId) {
        if (resId > 0) {
            ivBack.setVisibility(View.VISIBLE);
            ivBack.setImageResource(resId);
        }
    }

    /**
     * 设置标题栏右边图片资源
     * @param resId
     */
    protected void setTitleBarRightResource(int resId) {
        if (resId > 0) {
            ivAll.setVisibility(View.VISIBLE);
            ivAll.setImageResource(resId);
        }
    }

    protected void setLayout(View group, int ow, int w, int h) {
        int itemHeight = (int) (ow * h * 1.0f / w);
        ViewGroup.LayoutParams params;
        if (group.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            params = new LinearLayout.LayoutParams(ow, itemHeight);
        } else if (group.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
            params = new RelativeLayout.LayoutParams(ow, itemHeight);
        } else {
            params = new ViewGroup.LayoutParams(ow, itemHeight);
        }
        group.setLayoutParams(params);
    }


    protected void setMultiStateView(int state) {
        if (null != mMultiStateView) {
            mMultiStateView.setViewState(state);
        }
    }

    protected void setMultiStateView(int state, String empty) {
        if (null != mMultiStateView) {
            mMultiStateView.setViewState(state);
            ((TextView) mMultiStateView.getView(state).findViewById(R.id.tv_empty)).setText(empty);
        }
    }

    protected void setMultiStateViewListener(View.OnClickListener onClickListener){
        if (mMultiStateView != null && mMultiStateView.getView(MultiStateView.VIEW_STATE_ERROR) != null) {
            mMultiStateView.getView(MultiStateView.VIEW_STATE_ERROR).setOnClickListener(onClickListener);
        }
    }

    protected void showToast(String msg) {
        if (TextUtils.isEmpty(msg))
            return;
        Toasty.showShort(this, msg);
    }

    protected void showDialog(String msg) {
        loading.show();
        loading.setTitle(msg);
    }

    protected void dismissDialog() {
        if (null != loading) {
            loading.dismiss();
        }
    }

    /**
     * 强制关闭输入法
     */
    protected void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive() && this.getCurrentFocus() != null) {
            if (this.getCurrentFocus().getWindowToken() != null) {
                imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
    }

    /**
     * 自动弹出输入法
     */
    protected void showInputMethod() {
        //自动弹出键盘
        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    protected void onBackClickListener(View v) {
        hideKeyboard();
        onBackPressed();
    }

    protected void onRightClickListener(View v) {

    }

    protected boolean isImmersionBarEnabled() {
        return true;
    }

    protected abstract int getLayoutResId();

    protected abstract void initView(@Nullable Bundle savedInstanceState);

    protected void loadData() {

    }

}
